﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Reflection;
using System.Windows.Forms;
using HML.Design;

namespace HML
{
    /// <summary>
    /// 分块进度控件
    /// </summary>
    [Description("分块进度控件")]
    [DefaultProperty("PieceCount")]
    [DefaultEvent("PieceStatusChanged")]
    [TypeConverter(typeof(PropertyOrderConverter))]
    [Designer(typeof(PieceProgressDesigner))]
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(PieceProgress), "Controls.PieceProgress.Resources.PieceProgress.bmp")]
    public class PieceProgress : DpiControl, ISkinObject
    {
        #region 主题

        private SkinObjectXmlMetadata skinObjectXmlMetadata = null;
        /// <summary>
        /// 主题对象在主题文件信息
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual SkinObjectXmlMetadata SkinObjectXmlMetadata
        {
            get
            {
                if (this.skinObjectXmlMetadata == null)
                    this.skinObjectXmlMetadata = new SkinObjectXmlMetadata(Assembly.GetAssembly(typeof(PieceProgress)).GetName().Name, typeof(PieceProgress).Name);

                return this.skinObjectXmlMetadata;
            }
        }

        private SkinController skinController = null;
        /// <summary>
        /// 主题控制器
        /// </summary>
        [Description("主题控制器")]
        [Category("杂项")]
        [PropertyOrder(-360)]
        [DefaultValue(null)]
        [RefreshProperties(RefreshProperties.All)]
        public virtual SkinController SkinController
        {
            get { return this.skinController; }
            set
            {
                if (this.skinController == value)
                    return;

                if (this.skinController != null)
                    this.skinController.RemoveSkinObject(this);

                this.skinController = value;

                if (this.skinController != null)
                    this.skinController.AddSkinObject(this);
            }
        }

        private SkinEnabledState skinEnabled= SkinEnabledState.Auto;
        /// <summary>
        /// 主题是否启用
        /// </summary>
        [Description("主题是否启用")]
        [Category("杂项")]
        [PropertyOrder(-300)]
        [DefaultValue(SkinEnabledState.Auto)]
        [RefreshProperties(RefreshProperties.All)]
        public virtual SkinEnabledState SkinEnabled
        {
            get { return this.skinEnabled; }
            set
            {
                if (this.skinEnabled == value)
                    return;

                this.skinEnabled = value;
                this.OnSkinChanged();
            }
        }

        private SkinStyle skinStyle = SkinStyle.Normal;
        /// <summary>
        /// 主题风格
        /// </summary>
        [Description("主题风格")]
        [Category("杂项")]
        [PropertyOrder(-260)]
        [DefaultValue(SkinStyle.Normal)]
        [RefreshProperties(RefreshProperties.All)]
        public virtual SkinStyle SkinStyle
        {
            get { return this.skinStyle; }
            set
            {
                if (this.skinStyle == value)
                    return;

                this.skinStyle = value;
                this.OnSkinChanged();
            }
        }

        private StyleAppearanceObject stateAppearance;
        /// <summary>
        /// 风格外观
        /// </summary>
        [Description("风格外观")]
        [Category("杂项")]
        [PropertyOrder(-200)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public StyleAppearanceObject StyleAppearance
        {
            get
            {
                if (this.stateAppearance == null)
                    this.stateAppearance = new StyleAppearanceObject(this, null);
                return this.stateAppearance;
            }
        }

        /// <summary>
        /// 主题已更改
        /// </summary>
        public virtual void OnSkinChanged()
        {
            SkinManager.SyncSkinValueToProperty(this);
            this.Invalidate();
        }

        private ISkinObject followSkinObject = null;
        /// <summary>
        /// 主题设置跟随指定主题对象（自己的设置不再生效，提供给代码使用）
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Localizable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual ISkinObject FollowSkinObject
        {
            get { return this.followSkinObject; }
            set
            {
                if (this.followSkinObject == value)
                    return;

                this.followSkinObject = value;
                this.OnSkinChanged();
            }
        }

        public virtual bool GetSkinObjectSkinStateCore()
        {
            return SkinManager.GetSkinObjectSkinState(this.FollowSkinObject ?? this);
        }

        void ISkinObject.InitializeInvalidate()
        {
            throw new NotImplementedException();
        }

        void ISkinObject.Invalidate()
        {
            this.Invalidate();
        }

        #endregion

        #region 新增事件

        public delegate void SinglePieceStatusChangedEventHandler(object sender, PieceProgressStatusChangedEventArgs e);
        private static readonly object EventPieceStatusChanged = new object();
        /// <summary>
        /// 块状态更改事件
        /// </summary>
        [Description("块状态更改事件")]
        public event SinglePieceStatusChangedEventHandler PieceStatusChanged
        {
            add { Events.AddHandler(EventPieceStatusChanged, value); }
            remove { Events.RemoveHandler(EventPieceStatusChanged, value); }
        }

        #endregion

        #region 停用事件

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler PaddingChanged
        {
            add { base.PaddingChanged += value; }
            remove { base.PaddingChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler TabIndexChanged
        {
            add { base.TabIndexChanged += value; }
            remove { base.TabIndexChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler TabStopChanged
        {
            add { base.TabStopChanged += value; }
            remove { base.TabStopChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler RightToLeftChanged
        {
            add { base.RightToLeftChanged += value; }
            remove { base.RightToLeftChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler ImeModeChanged
        {
            add { base.ImeModeChanged += value; }
            remove { base.ImeModeChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler BackgroundImageChanged
        {
            add { base.BackgroundImageChanged += value; }
            remove { base.BackgroundImageChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler BackgroundImageLayoutChanged
        {
            add { base.BackgroundImageLayoutChanged += value; }
            remove { base.BackgroundImageLayoutChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler BackColorChanged
        {
            add { base.BackColorChanged += value; }
            remove { base.BackColorChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler ForeColorChanged
        {
            add { base.ForeColorChanged += value; }
            remove { base.ForeColorChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler Click
        {
            add { base.Click += value; }
            remove { base.Click -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler DoubleClick
        {
            add { base.DoubleClick += value; }
            remove { base.DoubleClick -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event MouseEventHandler MouseClick
        {
            add { base.MouseClick += value; }
            remove { base.MouseClick -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event MouseEventHandler MouseDoubleClick
        {
            add { base.MouseDoubleClick += value; }
            remove { base.MouseDoubleClick -= value; }
        }
        #endregion

        #region 新增属性

        private int pieceSize = 8;
        /// <summary>
        /// 块大小(像素)
        /// </summary>
        [Description("块大小(像素)")]
        [PropertyOrder(-198)]
        [DefaultValue(8)]
        public int PieceSize
        {
            get { return this.pieceSize; }
            set
            {
                if (this.pieceSize == value || value <= 0)
                    return;

                this.pieceSize = value;
                this.Invalidate();
            }
        }

        private int pieceAmount = 0;
        /// <summary>
        /// 块数量
        /// </summary>
        [Description("块数量")]
        [PropertyOrder(-196)]
        [DefaultValue(0)]
        public int PieceAmount
        {
            get { return this.pieceAmount; }
            set
            {
                if (this.pieceAmount == value || value < 0)
                    return;

                this.pieceAmount = value;
                this.InitializePiece(this.pieceAmount);
                this.Invalidate();
            }
        }

        #endregion

        #region 重写属性

        protected override Size DefaultSize
        {
            get { return new Size(200, 100); }
        }

        protected override ImeMode DefaultImeMode
        {
            get { return ImeMode.Disable; }
        }

        #endregion

        #region 停用属性

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new Padding Padding
        {
            get { return base.Padding; }
            set { base.Padding = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new int TabIndex
        {
            get { return base.TabIndex; }
            set { base.TabIndex = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new bool TabStop
        {
            get { return base.TabStop; }
            set { base.TabStop = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override string Text
        {
            get { return base.Text; }
            set { base.Text = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override Font Font
        {
            get { return base.Font; }
            set { base.Font = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override RightToLeft RightToLeft
        {
            get { return base.RightToLeft; }
            set { base.RightToLeft = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new ImeMode ImeMode
        {
            get { return base.ImeMode; }
            set { base.ImeMode = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override Image BackgroundImage
        {
            get { return base.BackgroundImage; }
            set { base.BackgroundImage = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override ImageLayout BackgroundImageLayout
        {
            get { return base.BackgroundImageLayout; }
            set { base.BackgroundImageLayout = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override Color BackColor
        {
            get { return this.StyleAppearance.BackColor; }
            set { }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override Color ForeColor
        {
            get { return this.StyleAppearance.Piece.Normal.BackColor; }
            set { }
        }

        #endregion

        #region 字段

        /// <summary>
        /// 块集合
        /// </summary>
        private List<PieceProgressStatus> pieceList = new List<PieceProgressStatus>();

        #endregion

        public PieceProgress()
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.StandardClick, false);
            SetStyle(ControlStyles.StandardDoubleClick, false);
            SetStyle(ControlStyles.Selectable, false);

            this.TabStop = false;
            this.InitializePiece(this.PieceAmount);
            this.OnSkinChanged();


        }

        #region 重写

        protected override void OnScaleDpiChangedInitialize()
        {
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (this.pieceList.Count < 1)
                return;

            Graphics g = e.Graphics;

            int border = 1;
            int scale_pieceSize = (int)Math.Ceiling(this.PieceSize * this.ScaleDpi);
            List<PieceProgressStatus> pieceList_tmp = this.pieceList;
            int col = Math.Max(1, (this.ClientRectangle.Width - border) / (scale_pieceSize + border));
            int col_remainder = pieceList_tmp.Count % col;
            int row = pieceList_tmp.Count / col;

            //网格
            SolidBrush grid_sb = new SolidBrush(this.StyleAppearance.GridColor);
            g.FillRectangle(grid_sb, new RectangleF(this.ClientRectangle.X, this.ClientRectangle.Y, (scale_pieceSize + border) * col + border, (scale_pieceSize + border) * row + border));
            if (col_remainder > 0)
            {
                g.FillRectangle(grid_sb, new RectangleF(this.ClientRectangle.X, this.ClientRectangle.Y + (scale_pieceSize + border) * row + border, (scale_pieceSize + border) * col_remainder + border, scale_pieceSize + border));
            }
            grid_sb.Dispose();

            //块
            SolidBrush piece_normal_sb = new SolidBrush(this.StyleAppearance.Piece.Normal.BackColor);
            SolidBrush piece_executing_sb = new SolidBrush(this.StyleAppearance.Piece.Executing.BackColor);
            SolidBrush piece_warn_sb = new SolidBrush(this.StyleAppearance.Piece.Warn.BackColor);
            SolidBrush piece_success_sb = new SolidBrush(this.StyleAppearance.Piece.Success.BackColor);
            SolidBrush piece_error_sb = new SolidBrush(this.StyleAppearance.Piece.Error.BackColor);

            for (int i = 0; i < row + (col_remainder > 0 ? 1 : 0); i++)
            {
                for (int j = 0; j < col; j++)
                {
                    int index = i * col + j;
                    if (index > pieceList_tmp.Count - 1)
                    {
                        return;
                    }
                    Rectangle piece_rect = new Rectangle(this.ClientRectangle.X + border * (j + border) + scale_pieceSize * j, this.ClientRectangle.Y + border * (i + border) + scale_pieceSize * i, scale_pieceSize, scale_pieceSize);
                    if (pieceList_tmp[index] == PieceProgressStatus.Normal)
                    {
                        g.FillRectangle(piece_normal_sb, piece_rect);
                    }
                    else if (pieceList_tmp[index] == PieceProgressStatus.Executing)
                    {
                        g.FillRectangle(piece_executing_sb, piece_rect);
                    }
                    else if (pieceList_tmp[index] == PieceProgressStatus.Warn)
                    {
                        g.FillRectangle(piece_warn_sb, piece_rect);
                    }
                    else if (pieceList_tmp[index] == PieceProgressStatus.Success)
                    {
                        g.FillRectangle(piece_success_sb, piece_rect);
                    }
                    else if (pieceList_tmp[index] == PieceProgressStatus.Error)
                    {
                        g.FillRectangle(piece_error_sb, piece_rect);
                    }
                }
            }

            piece_normal_sb.Dispose();
            piece_executing_sb.Dispose();
            piece_warn_sb.Dispose();
            piece_success_sb.Dispose();
            piece_error_sb.Dispose();

        }

        #endregion

        #region 虚方法

        protected virtual void OnPieceStatusChanged(PieceProgressStatusChangedEventArgs e)
        {
            SinglePieceStatusChangedEventHandler handler = Events[EventPieceStatusChanged] as SinglePieceStatusChangedEventHandler;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        #endregion

        #region 公开方法

        /// <summary>
        /// 获取指定块的状态
        /// </summary>
        /// <param name="index">块索引</param>
        public PieceProgressStatus GetPieceStatus(int index)
        {
            if (index < 0 && index >= this.pieceList.Count)
            {
                throw new IndexOutOfRangeException("索引超出范围");
            }
            return this.pieceList[index];
        }

        /// <summary>
        /// 设置指定块的状态
        /// </summary>
        /// <param name="index">块索引</param>
        /// <param name="status">要设置块状态</param>
        public void SetPieceStatus(int index, PieceProgressStatus status)
        {
            if (index < 0 && index >= this.pieceList.Count && this.pieceList[index] != status)
                return;

            ArrayList indexArr = new ArrayList() { index };
            ArrayList oldstatusArr = new ArrayList() { this.pieceList[index] };
            ArrayList newstatusArr = new ArrayList() { status };
            this.pieceList[index] = status;

            this.Invalidate();
            this.OnPieceStatusChanged(new PieceProgressStatusChangedEventArgs(false, indexArr, oldstatusArr, newstatusArr));
        }

        /// <summary>
        /// 设置指定块的状态
        /// </summary>
        /// <param name="indexArr">块索引集合</param>
        /// <param name="status">要设置块状态</param>
        public void SetPieceStatus(int[] indexArr, PieceProgressStatus status)
        {
            ArrayList _indexArr = new ArrayList();
            ArrayList _oldstatusArr = new ArrayList();
            ArrayList _newstatusArr = new ArrayList();

            List<PieceProgressStatus> pieceList_tmp = this.pieceList;
            for (int i = 0; i < indexArr.Length; i++)
            {
                if (pieceList_tmp[indexArr[i]] != status)
                {
                    _indexArr.Add(indexArr[i]);
                    _oldstatusArr.Add(pieceList_tmp[i]);
                    _newstatusArr.Add(status);
                    pieceList_tmp[indexArr[i]] = status;
                }
            }

            if (_indexArr.Count > 0)
            {
                this.Invalidate();
                this.OnPieceStatusChanged(new PieceProgressStatusChangedEventArgs(false, _indexArr, _oldstatusArr, _newstatusArr));
            }

        }

        /// <summary>
        /// 设置指定块的状态
        /// </summary>
        /// <param name="indexArr">块索引集合</param>
        /// <param name="statusArr">要设置块状态集合</param>
        public void SetPieceStatus(int[] indexArr, PieceProgressStatus[] statusArr)
        {
            ArrayList _indexArr = new ArrayList();
            ArrayList _oldstatusArr = new ArrayList();
            ArrayList _newstatusArr = new ArrayList();

            List<PieceProgressStatus> pieceList_tmp = this.pieceList;
            for (int i = 0; i < indexArr.Length; i++)
            {
                if (pieceList_tmp[indexArr[i]] != statusArr[i])
                {
                    _indexArr.Add(indexArr[i]);
                    _oldstatusArr.Add(pieceList_tmp[i]);
                    _newstatusArr.Add(statusArr[i]);
                    pieceList_tmp[indexArr[i]] = statusArr[i];
                }
            }

            if (_indexArr.Count > 0)
            {
                this.Invalidate();
                this.OnPieceStatusChanged(new PieceProgressStatusChangedEventArgs(false, _indexArr, _oldstatusArr, _newstatusArr));
            }

        }

        /// <summary>
        /// 设置所有块为统一状态
        /// </summary>
        /// <param name="status">要设置的状态</param>
        public void SetAllPieceStatus(PieceProgressStatus status)
        {
            if (this.pieceList.Count < 1)
                return;

            ArrayList indexArr = new ArrayList();
            ArrayList oldstatusArr = new ArrayList();
            ArrayList newstatusArr = new ArrayList();

            List<PieceProgressStatus> pieceList_tmp = this.pieceList;
            for (int i = 0; i < pieceList_tmp.Count; i++)
            {
                if (pieceList_tmp[i] != status)
                {
                    indexArr.Add(i);
                    oldstatusArr.Add(pieceList_tmp[i]);
                    newstatusArr.Add(status);
                    pieceList_tmp[i] = status;
                }
            }

            if (indexArr.Count > 0)
            {
                this.Invalidate();
                this.OnPieceStatusChanged(new PieceProgressStatusChangedEventArgs(true, indexArr, oldstatusArr, newstatusArr));
            }

        }

        /// <summary>
        /// 获取指定状态的块索引集合
        /// </summary>
        /// <param name="status">指定状态</param>
        /// <returns></returns>
        public ArrayList GetIndexList(PieceProgressStatus status)
        {
            ArrayList indexArr = new ArrayList();
            List<PieceProgressStatus> pieceList_tmp = this.pieceList;
            for (int i = 0; i < pieceList_tmp.Count; i++)
            {
                if (pieceList_tmp[i] == status)
                {
                    indexArr.Add(i);
                }
            }
            return indexArr;
        }

        /// <summary>
        /// 获取指定状态的块总数
        /// </summary>
        /// <param name="status">指定状态</param>
        /// <returns></returns>
        public int GetIndexListCount(PieceProgressStatus status)
        {
            int count = 0;
            List<PieceProgressStatus> pieceList_tmp = this.pieceList;
            for (int i = 0; i < pieceList_tmp.Count; i++)
            {
                if (pieceList_tmp[i] == status)
                {
                    count++;
                }
            }
            return count;
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 根据指定数量创建块集合
        /// </summary>
        /// <param name="count"></param>
        private void InitializePiece(int count)
        {
            List<PieceProgressStatus> new_pieceList = new List<PieceProgressStatus>();
            for (int i = 0; i < count; i++)
            {
                new_pieceList.Add(PieceProgressStatus.Normal);
            }
            this.pieceList = new_pieceList;
        }

        #endregion

        #region 外观

        public class StyleAppearanceObject : AppearanceObjectBase
        {
            internal StyleAppearanceObject(ISkinObject owner, AppearanceObjectBase parent) : base(owner, parent) { }

            #region 属性

            private Color skinBackColor = Color.Empty;
            private readonly Color defaultBackColor = SystemColors.Control;
            private Color backColor = Color.Empty;
            /// <summary>
            /// 背景颜色
            /// </summary>
            [Description("背景颜色")]
            [PropertyOrder(-198)]
            [SkinProperty(true, true)]
            public Color BackColor
            {
                get
                {
                    if (this.owner.GetSkinObjectSkinStateCore() && this.skinBackColor != Color.Empty)
                    {
                        return this.skinBackColor;
                    }

                    if (this.backColor != Color.Empty)
                    {
                        return this.backColor;
                    }

                    if (((Control)this.owner).Parent != null)
                    {
                        return ((Control)this.owner).Parent.BackColor;
                    }

                    return this.defaultBackColor;
                }
                set
                {
                    if (this.backColor == value)
                        return;

                    this.backColor = value;
                    this.Invalidate();
                }
            }
            private bool ShouldSerializeBackColor()
            {
                return this.backColor != Color.Empty;
            }
            private void ResetBackColor()
            {
                this.backColor = Color.Empty;
                this.Invalidate();
            }

            private Color skinGridColor = Color.Empty;
            private readonly Color defaultGridColor = Color.AliceBlue;
            private Color gridColor = Color.Empty;
            /// <summary>
            /// 网格颜色
            /// </summary>
            [Description("网格颜色")]
            [PropertyOrder(-196)]
            [SkinProperty(true, false)]
            public Color GridColor
            {
                get
                {
                    if (this.owner.GetSkinObjectSkinStateCore())
                    {
                        return this.skinGridColor;
                    }

                    if (this.gridColor != Color.Empty)
                    {
                        return this.gridColor;
                    }

                    return this.defaultGridColor;
                }
                set
                {
                    if (this.gridColor == value)
                        return;

                    this.gridColor = value;
                    this.Invalidate();
                }
            }
            private bool ShouldSerializeGridColor()
            {
                return this.gridColor != Color.Empty;
            }
            private void ResetGridColor()
            {
                this.gridColor = Color.Empty;
                this.Invalidate();
            }

            private PieceAppearanceObject piece;
            /// <summary>
            /// 块
            /// </summary>
            [Description("块")]
            [PropertyOrder(-194)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            [SkinProperty(true, false)]
            public PieceAppearanceObject Piece
            {
                get
                {
                    if (this.piece == null)
                        this.piece = new PieceAppearanceObject(this.owner, this);
                    return this.piece;
                }
            }

            #endregion

            public class PieceAppearanceObject : AppearanceObjectBase
            {
                internal PieceAppearanceObject(ISkinObject owner, AppearanceObjectBase parent) : base(owner, parent) { }

                #region 属性

                private ItemAppearanceObject normal;
                /// <summary>
                /// 正常状态
                /// </summary>
                [Description("正常状态")]
                [PropertyOrder(-194)]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
                [SkinProperty(true, false)]
                public ItemAppearanceObject Normal
                {
                    get
                    {
                        if (this.normal == null)
                            this.normal = new ItemAppearanceObject(this.owner, this, Color.LightSteelBlue);
                        return this.normal;
                    }
                }

                private ItemAppearanceObject executing;
                /// <summary>
                /// 处理中状态
                /// </summary>
                [Description("处理中状态")]
                [PropertyOrder(-192)]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
                [SkinProperty(true, false)]
                public ItemAppearanceObject Executing
                {
                    get
                    {
                        if (this.executing == null)
                            this.executing = new ItemAppearanceObject(this.owner, this, Color.DeepSkyBlue);
                        return this.executing;
                    }
                }

                private ItemAppearanceObject warn;
                /// <summary>
                /// 警告状态
                /// </summary>
                [Description("警告状态")]
                [PropertyOrder(-191)]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
                [SkinProperty(true, false)]
                public ItemAppearanceObject Warn
                {
                    get
                    {
                        if (this.warn == null)
                            this.warn = new ItemAppearanceObject(this.owner, this, Color.Yellow);
                        return this.warn;
                    }
                }

                private ItemAppearanceObject success;
                /// <summary>
                /// 成功状态
                /// </summary>
                [Description("成功状态")]
                [PropertyOrder(-190)]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
                [SkinProperty(true, false)]
                public ItemAppearanceObject Success
                {
                    get
                    {
                        if (this.success == null)
                            this.success = new ItemAppearanceObject(this.owner, this, Color.YellowGreen);
                        return this.success;
                    }
                }

                private ItemAppearanceObject error;
                /// <summary>
                /// 错误状态
                /// </summary>
                [Description("错误状态")]
                [PropertyOrder(-188)]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
                [SkinProperty(true, false)]
                public ItemAppearanceObject Error
                {
                    get
                    {
                        if (this.error == null)
                            this.error = new ItemAppearanceObject(this.owner, this, Color.Tomato);
                        return this.error;
                    }
                }

                #endregion

                public class ItemAppearanceObject : AppearanceObjectBase
                {
                    internal ItemAppearanceObject(ISkinObject owner, AppearanceObjectBase parent, Color color) : base(owner, parent)
                    {
                        this.defaultBackColor = color;
                    }

                    #region 属性

                    private Color skinBackColor = Color.Empty;
                    private readonly Color defaultBackColor = Color.Empty;
                    private Color backColor = Color.Empty;
                    /// <summary>
                    /// 背景颜色
                    /// </summary>
                    [Description("背景颜色")]
                    [PropertyOrder(-158)]
                    [SkinProperty(true, false)]
                    public Color BackColor
                    {
                        get
                        {
                            if (this.owner.GetSkinObjectSkinStateCore())
                            {
                                return this.skinBackColor;
                            }

                            if (this.backColor != Color.Empty)
                            {
                                return this.backColor;
                            }

                            return this.defaultBackColor;
                        }
                        set
                        {
                            if (this.backColor == value)
                                return;

                            this.backColor = value;
                            this.Invalidate();
                        }
                    }
                    private bool ShouldSerializeBackColor()
                    {
                        return this.backColor != Color.Empty;
                    }
                    private void ResetBackColor()
                    {
                        this.backColor = Color.Empty;
                        this.Invalidate();
                    }

                    #endregion

                }

            }

        }

        #endregion

    }
}
